package com.youlu.campus.website.service.impl;

import com.youlu.campus.website.entity.WebsiteConfig;
import com.youlu.campus.website.repositories.WebsiteConfigRepository;
import com.youlu.campus.website.service.WebsiteConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 官网配置
 */
@Slf4j
@Service
public class WebsiteConfigServiceImpl implements WebsiteConfigService {

    @Autowired
    private WebsiteConfigRepository websiteConfigRepository;

    /**
     * 新增 官网配置
     *
     * @param jumpConfig
     * @return
     */
    @Override
    public WebsiteConfig save(WebsiteConfig jumpConfig) {
        WebsiteConfig one = this.findOne();
        if (Objects.nonNull(one)) {
            jumpConfig.setId(one.getId());
            jumpConfig.setUpdateTime(LocalDateTime.now());
        } else {
            jumpConfig.setCreateTime(LocalDateTime.now());
        }
        return websiteConfigRepository.save(jumpConfig);
    }


    /**
     * 查询 官网配置
     *
     * @param
     * @return
     */
    @Override
    public WebsiteConfig findOne() {
        List<WebsiteConfig> all = websiteConfigRepository.findAll();
        if (CollectionUtils.isEmpty(all)) {
            return null;
        }
        return all.get(0);
    }

}